package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.po.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 从容器中获取对象
     */
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public User login(@RequestParam String username, @RequestParam String password){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>( );
        queryWrapper.eq(User::getUsername,username).eq(User::getPassword,password);
        List<User> list=userService.list(queryWrapper);
        if(list == null || list.size() == 0){
            System.out.println("用户名或密码错误" );
            return null;
        }else{
            System.out.println("登录成功" );
            return list.get(0);
        }
    }

    /**
     * http://localhost:8080/user/list
     * @return
     */
    @RequestMapping("/list")
    public List<User> list(){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        List list = userService.list(queryWrapper);
        return list;
    }

    @RequestMapping("/saveOrUpdate")
    public boolean saveOrUpdate(@RequestBody User user){
        boolean success = userService.saveOrUpdate(user);
        return success;
    }

    @RequestMapping("/getById/{id}")
    public User getById(@PathVariable String id){
        User user  = userService.getById(id);
        return user;
    }

    @RequestMapping("/deleteById/{id}")
    public boolean deleteById(@PathVariable  String id){
        boolean success = userService.removeById(id);
        return success;
    }

    @RequestMapping("/getbydeptid")
    public List<User> getbydeptid(@RequestParam Integer deptid){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deptid",deptid).eq("usetype",3);
        return userService.list(queryWrapper);
    }
}